package com.jieshuibao.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.jieshuibao.mapper.CounselorMapper;
import com.jieshuibao.mapper.UserExMapper;
import com.jieshuibao.model.db.Counselor;
import com.jieshuibao.model.db.CounselorExample;
import com.jieshuibao.model.db.UserSearch;
import com.jieshuibao.model.domain.OnlookReq;
import com.jieshuibao.model.domain.SerarchExample;
import com.jieshuibao.util.StringUtils;

@Service
public class CounselorService{
	
	@Resource
	private CounselorMapper counselorMapper;
	
	@Resource
	private UserExMapper userExMapper;
	
	@Resource
	private CounselorDutyService counselorDutyService;
	
	public void addCounselor(Counselor counselor) {
		counselorMapper.insertSelective(counselor);
	}
	
	public void delCounselorByUuid(String uuid) {
		counselorMapper.deleteByPrimaryKey(uuid);
	}
	
	public void updateCounselor(Counselor counselor) {
		counselorMapper.updateByPrimaryKeySelective(counselor);
	}
	
	public Counselor getCounselorByUuid(String uuid) {
		return counselorMapper.selectByPrimaryKey(uuid);
	}
	
	public List<Counselor> getPageCounselor(CounselorExample counselorExample) {
		return counselorMapper.selectPage(counselorExample);
	}
	
	public int countCounselor(CounselorExample counselorExample) {
		return counselorMapper.countByExample(counselorExample);
	}
	
	/**
	 * 咨询师列表
	 * @param example
	 * @return
	 */
	public List<UserSearch> getCounselorList(SerarchExample example){
		return userExMapper.selectCounselorUser(example);
	}
	
	public List<UserSearch> getCounselorList(OnlookReq req){
		String content = req.getContent();
		int sinceId = req.getSinceId();
		int maxId = req.getMaxId();
		
		SerarchExample serarchExample = new SerarchExample();
		serarchExample.setPageIndex(sinceId - 1);
		serarchExample.setPageSize(maxId - sinceId + 1);
		serarchExample.setOrderByClause("counselor.recommendNo desc,counselor.applyDate desc");
		if (StringUtils.isNotBlank(content)) {
			serarchExample.setSerarch(content);
		}
		List<UserSearch> list = userExMapper.selectCounselorUser(serarchExample);
		for (UserSearch userSearch : list) {
			String counselorDuty = userSearch.getCounselorDuty();
			if (StringUtils.isNotBlank(counselorDuty)) {
				String counselorDutyName = counselorDutyService.getCounselorDutyName(counselorDuty);
				userSearch.setCounselorDuty(counselorDutyName);
			}
			String adept = userSearch.getAdept();
			if (StringUtils.isNotBlank(adept)) {
				String selectAdeptName = userExMapper.selectAdeptName(adept);
				userSearch.setAdept(selectAdeptName);
			}
			
		}
		return list;
	}
	
	/**
	 * 
	 * @param content 与serarch 二选择一个
	 * @param sinceId
	 * @param maxId
	 * @param serarch
	 * @return
	 */
	public List<UserSearch> getCounselorList(List<String> content,int sinceId,int maxId,String serarch){
	
		
		SerarchExample serarchExample = new SerarchExample();
		serarchExample.setPageIndex(sinceId - 1);
		serarchExample.setPageSize(maxId - sinceId + 1);
		
		if (StringUtils.isNotBlank(serarch)) {
			serarchExample.setOrderByClause("counselor.recommendNo desc,counselor.applyDate desc");
			serarchExample.setSerarch(serarch);
		}else if (content!=null) {
			serarchExample.setSerarchList(content);
		}else {
			serarchExample.setOrderByClause("counselor.recommendNo desc,counselor.applyDate desc");
		}
		List<UserSearch> list = userExMapper.selectCounselorUser(serarchExample);
		for (UserSearch userSearch : list) {
			String counselorDuty = userSearch.getCounselorDuty();
			if (StringUtils.isNotBlank(counselorDuty)) {
				String counselorDutyName = counselorDutyService.getCounselorDutyName(counselorDuty);
				userSearch.setCounselorDuty(counselorDutyName);
			}
			String adept = userSearch.getAdept();
			if (StringUtils.isNotBlank(adept)) {
				String selectAdeptName = userExMapper.selectAdeptName(adept);
				userSearch.setAdept(selectAdeptName);
			}
			
		}
		return list;
	}
	/**
	 * 获取咨询师的擅长业务
	 * @param adept
	 * @return
	 */
	public String getCounselorAdepts(String adept){
		return userExMapper.selectAdeptName(adept);
		
	}
}
